var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },
  goodsCarNumber: 0,
  tDetail:function(e){
    const goodsId = e.currentTarget.dataset.goodsid
    if (!this.data.buttonClicked) {
      util.buttonClicked(this);
      const goodsId = e.currentTarget.dataset.goodsid;
      wx.navigateTo({
        url: '../commodityDetail/commodityDetail?goodsId=' + goodsId
      })
    }
  },
  getInCar: function (e) {
    var that = this;
    const goodsId = e.currentTarget.dataset.goodsid;
    const goodsName = e.currentTarget.dataset.goodsname;
    const shopPrice = e.currentTarget.dataset.shopprice;
    const originalImg = e.currentTarget.dataset.originalimg;
    const userId = app.globalData.userid
    wx.request({
      url: app.globalData.url + 'insertGoodsInCartShop',
      data: {
        goodsNumber: 1,
        goodsPic: originalImg,
        goodsName: goodsName,
        goodsPrice: shopPrice,
        goodsId: goodsId,
        cartType: 2,
        userId: userId,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        wx.showToast({
          title: '购物车添加成功',
          icon: 'success',
          duration: 1000,
          mask: true,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })

        that.queryCarList();
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //上面商品块儿
    wx.request({
      url: app.globalData.url + 'getRightTopPromote',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          goodsUp: res.data.object
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    //下面商品块儿
    wx.request({
      url: app.globalData.url + 'getRightBottomPromote',
      data: { },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          goods: res.data.object
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    that.queryCarList();
  },
  onShow:function(){
    this.queryCarList();
  },
  /**
   * 横向滑动
   */
  scrollToTop: function (e) {
    this.setAction({
      scrollTop: 0
    })
  },
  tap: function (e) {
    for (var i = 0; i < order.length; ++i) {
      if (order[i] === this.data.toView) {
        this.setData({
          toView: order[i + 1],
          scrollTop: (i + 1) * 200
        })
        break
      }
    }
  },
  /**首页购物车跳转 */
  InshoppingCar: function () {
    wx.navigateTo({
      url: '../shoppingCar/shoppingCar',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**查询购物车列表 */
  queryCarList: function () {
    const that = this;
    const userId = app.globalData.userid
    wx.request({
      url: app.globalData.url + 'getShopCart',
      data: {
        userId: userId,
        cartType: 2,
        pageIndex: 1,
        pageSize: 100
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.goodsCarNumber = 0
        if (res.data.object.cartList != undefined) {

          for (let i = 0; i < res.data.object.cartList.length; i++) {
            that.goodsCarNumber += res.data.object.cartList[i].goodsNumber
          }
          that.setData({
            goodsCarNumber: that.goodsCarNumber
          })
        } else {
          that.setData({
            goodsCarNumber: 0
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  }
})